#include "Memory.h"
#include <iostream>
#include "Variable.h"
#include "Data.h"
#include <cassert>
#include "DataHolderGarbageCollector.h"

using namespace std;

Memory::Memory() {
	DHGC->addReferencer(this);
}

Memory::~Memory() {
	DHGC->removeReferencer(this);
}

void Memory::reset() {
	m_variables.clear();
}

void Memory::addVariable(Variable* newVar) {
	assert(newVar);
	
	bool variableAlreadyExists = getVariable(newVar->getName());
	assert(!variableAlreadyExists);
	
	m_variables.push_back(newVar);
}

Variable* Memory::getVariable(const string& name) {
	assert(name != "");
	
	Variable* foundVariable = NULL;
	
	for (Variables::iterator i = m_variables.begin(); i != m_variables.end(); ++i) {
		Variable* var = (*i);
		if (var->getName() == name) {
			foundVariable = var;
			break;
		}
	}
	
	return foundVariable;
}

void Memory::setDataHoldersToReferenced() {

	for (Variables::iterator i = m_variables.begin(); i != m_variables.end(); ++i) {
		Variable* var = (*i);
		var->setReferenced(true);
	}
}